package com.codeoss.framework.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.db.util.OracleDataConvertUtil;
import com.codeoss.framework.response.MMenuResponse;
import com.codeoss.framework.service.IMMenuService;
import com.codeoss.framework.util.IOUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.model.HierarchyData;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.module.ir.model.IrMobileMenu;
import com.codeoss.module.ir.service.IIrMobileMenuService;

@Service
public class MMenuService implements IMMenuService {
    @Autowired
    private IIrMobileMenuService irMobileMenuService;
    @Autowired
	private DataSourceUtil dataSourceUtil;
    @Value("${framework.db-type}")
	private String dbType;
	@Override
	public MMenuResponse init() {
		// TODO Auto-generated method stub
		MMenuResponse mMenuResponse=new MMenuResponse();
		List<Map<String,Object>> dataList=irMobileMenuService.getCanAccessMobileMenuList();
		if(dbType.equals("oracle")) {
			dataList=OracleDataConvertUtil.doConvert(dataList);
		}
		YunContext yunContext=YunContext.getCurrentContext();
		HierarchyData menu=dataSourceUtil.convertTableDataToHierarchyData(yunContext, dataList);
		
		List<HierarchyData> list=new ArrayList<HierarchyData>();
		list.add(menu);
		
		for(int i=menu.getChildren().size()-1;i>=0;i--) {
			HierarchyData moduleData=menu.getChildren().get(i);
			if(moduleData.getChildren()==null ||moduleData.getChildren().size()==0) {
				menu.getChildren().remove(i);
				continue;
			}
		}
		
		mMenuResponse.setMenu(menu);
		return mMenuResponse;
	}
	
	@Override
	public MMenuResponse getPageCodeByMenuId(Long menuId) {
		// TODO Auto-generated method stub
		MMenuResponse mMenuResponse=new MMenuResponse();
		IrMobileMenu irMobileMenu=irMobileMenuService.getById(menuId);
		mMenuResponse.setMenuType(irMobileMenu.getMenuType());
		mMenuResponse.setPageCode(irMobileMenu.getPageCode());
		return mMenuResponse;
	}

	@Override
	public MMenuResponse getMenuIconContent(Long menuId) {
		// TODO Auto-generated method stub
		MMenuResponse mMenuResponse=new MMenuResponse();
		IrMobileMenu irMobileMenu=irMobileMenuService.getById(menuId);
		String icon=irMobileMenu.getIcon();
		if(!StringUtils.isEmpty(icon)) {
			if(icon.startsWith("classpath:")) {
				icon=icon.substring("classpath:".length());
				InputStream is = this.getClass().getClassLoader().getResourceAsStream(icon);
				ByteArrayOutputStream os=new ByteArrayOutputStream();
				byte b[]=new byte[1024];
				int len=0;
				try {
					while((len=is.read(b))!=-1) {
						os.write(b, 0, len);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					throw new RuntimeException(e);
				}finally {
					IOUtil.closeInputStream(is);
				}
				mMenuResponse.setMenuIconContent(os.toByteArray());
			}
		}
		return mMenuResponse;
	}
	
}
